package com.qxzn.app.page.model;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.TuyaTokenBean;
import com.qxzn.app.bean.TuyaUidBean;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.util.AppUtil;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SmartHomeModel {

    private Context context;
    private MyInterface.GetTuyaUidListener getTuyaUidListener;
    private MyInterface.GetTuyaTokenListener getTuyaTokenListener;
    private MyInterface.NotifyServerListener notifyServerListener;
    private Call call;

    public SmartHomeModel(Context context) {
        this.context = context;
    }

    /**
     * 获取涂鸦UID
     *
     * @param params
     */
    public void getTuyaUid(JSONObject params) {
        if (params == null) {
            return;
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), params.toJSONString());
        call = HttpRequest.getService().getTuyaUid(body);
        call.enqueue(new Callback<BaseBean<TuyaUidBean>>() {
            @Override
            public void onResponse(Call<BaseBean<TuyaUidBean>> call, Response<BaseBean<TuyaUidBean>> response) {
                if (getTuyaUidListener != null) {
                    getTuyaUidListener.onGetTuyaUidSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean<TuyaUidBean>> call, Throwable t) {
                if (getTuyaUidListener != null) {
                    getTuyaUidListener.onGetTuyaUidFailure();
                }
                AppUtil.showToast(context, "网络错误，请稍后再试！");
                AppUtil.showLog("错误：" + t);
            }
        });
    }

    /**
     * 获取涂鸦Token
     *
     * @param params
     */
    public void getTuyaToken(JSONObject params) {
        if (params == null) {
            return;
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), params.toJSONString());
        call = HttpRequest.getService().getTuyaToken(body);
        call.enqueue(new Callback<BaseBean<TuyaTokenBean>>() {
            @Override
            public void onResponse(Call<BaseBean<TuyaTokenBean>> call, Response<BaseBean<TuyaTokenBean>> response) {
                if (getTuyaTokenListener != null) {
                    getTuyaTokenListener.onGetTuyaTokenSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean<TuyaTokenBean>> call, Throwable t) {
                if (getTuyaTokenListener != null) {
                    getTuyaTokenListener.onGetTuyaTokenFailure();
                }
                AppUtil.showToast(context, "网络错误，请稍后再试！");
                AppUtil.showLog("错误：" + t);
            }
        });
    }

    /**
     * 通知服务器配网成功
     *
     * @param params
     */
    public void notifyServer(JSONObject params) {
        if (params == null) {
            return;
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), params.toJSONString());
        call = HttpRequest.getService().notifyServer(body);
        call.enqueue(new Callback<BaseBean<TuyaTokenBean>>() {
            @Override
            public void onResponse(Call<BaseBean<TuyaTokenBean>> call, Response<BaseBean<TuyaTokenBean>> response) {
                if (notifyServerListener != null) {
                    notifyServerListener.onNotifyServerSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean<TuyaTokenBean>> call, Throwable t) {
                if (notifyServerListener != null) {
                    notifyServerListener.onNotifyServerFailure();
                }
                AppUtil.showToast(context, "网络错误，请稍后再试！");
                AppUtil.showLog("错误：" + t);
            }
        });
    }

    /**
     * 在页面关闭时移除监听
     */
    public void removeListener() {
        if (call != null) {
            call.cancel();
        }
        this.getTuyaUidListener = null;
        this.getTuyaTokenListener = null;
        this.notifyServerListener = null;
    }

    public void setGetTuyaUidListener(MyInterface.GetTuyaUidListener getTuyaUidListener) {
        this.getTuyaUidListener = getTuyaUidListener;
    }

    public void setGetTuyaTokenListener(MyInterface.GetTuyaTokenListener getTuyaTokenListener) {
        this.getTuyaTokenListener = getTuyaTokenListener;
    }

    public void setNotifyServerListener(MyInterface.NotifyServerListener notifyServerListener) {
        this.notifyServerListener = notifyServerListener;
    }

}
